﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WorkSchedueSoft.Data;
using System.Windows.Forms;
using Timer = System.Threading.Timer;

namespace WorkSchedueSoft
{
    class SchedueProcess : Singleton<SchedueProcess>
    {

        Timer timer;

        Queue<SchedueCommandItem> commands;

        bool is_last_progress_start_complete = true;

        public override void Init()
        {
            base.Init();

            commands = new Queue<Data.SchedueCommandItem>();
            timer = new Timer(new TimerCallback(timerCall), this, 0, 100);

        }

        private void timerCall(object state)
        {

            if (commands.Count > 0)
            {
                if (is_last_progress_start_complete)
                {
                    is_last_progress_start_complete = false;

                    StartAExe(commands.Dequeue());

                }
            }
        }

        public void RunSchedue(SchedueItem target)
        {
            if (target.commands.Count > 0)
            {
                foreach (var item in target.commands)
                {
                    commands.Enqueue(item);
                }
            }

        }

        private void StartAExe(SchedueCommandItem command_item)
        {

            Thread worker = new Thread(delegate () {

                Thread.Sleep(command_item.delay);

                try
                {
                    if (command_item.tip_for_complete)
                    {

                        if (MessageBox.Show(command_item.description, "准备好了点确定进入下一步", MessageBoxButtons.OKCancel) == DialogResult.OK)
                        {
                            StartProcess(command_item);
                            is_last_progress_start_complete = true;
                        }
                        else
                        {
                            MessageBox.Show("操作终止");
                            commands.Clear();
                            is_last_progress_start_complete = true;
                        }
                    }
                    else
                    {
                        StartProcess(command_item);
                        is_last_progress_start_complete = true;
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show(command_item.path + " " + e.Message);
                    return;
                }
               
            });
            worker.Start();

        }

        private static void StartProcess(SchedueCommandItem command_item)
        {
            //使用默认操作打开目标文件或路径
            if (string.IsNullOrEmpty(command_item.boot_source_path))
            {
                System.Diagnostics.Process.Start(command_item.path, command_item.boot_para);

            }
            else
            {
                //指定某款程序打开某个文件
                System.Diagnostics.Process.Start(command_item.boot_source_path, command_item.path);
            }
        }
    }
}
